package com.yang.system.domain.orgMng.org.validator;

import com.yang.system.application.orgMng.orgService.OrgResponse;
import com.yang.system.domain.common.validator.CommonValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrgValidator {
    //依赖更小的 Validator，也就是更小的规则分组
    private final CommonValidator commonValidator;
    private final OrgTypeValidator orgTypeValidator;
    private final SuperiorValidator superiorValidator;
    private final OrgNameValidator orgNameValidator;
    private final OrgLeaderValidator orgLeaderValidator;

    @Autowired
    public OrgValidator(CommonValidator commonValidator, OrgTypeValidator orgTypeValidator, SuperiorValidator superiorValidator, OrgNameValidator orgNameValidator, OrgLeaderValidator orgLeaderValidator) {
        this.commonValidator = commonValidator;
        this.orgTypeValidator = orgTypeValidator;
        this.superiorValidator = superiorValidator;
        this.orgNameValidator = orgNameValidator;
        this.orgLeaderValidator = orgLeaderValidator;
    }

    public void validate(OrgResponse request) {
        final Long tenantId = request.getTenantId();

        commonValidator.tenantShouldValid(tenantId);
        orgLeaderValidator.verify(tenantId, request.getSuperiorId());
        orgTypeValidator.verify(tenantId, request.getOrgTypeCode());
        superiorValidator.verify(tenantId, request.getSuperiorId()
                , request.getOrgTypeCode());
        orgNameValidator.verify(tenantId, request.getName()
                , request.getSuperiorId());
    }
}
